package com.mzpai.board.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mzpai.board.mapper.OperateFlowLogMapper;
import com.mzpai.board.model.OperateFlowLog;
import com.mzpai.board.model.OperateLog;
import com.mzpai.board.service.OperateFlowLogService;
import com.mzpai.board.service.OperateLogService;
import com.mzpai.board.vo.OperateFlowLogReqVo;
import com.mzpai.service.impl.BaseServiceImpl;
import com.mzpai.util.DateUtil;
import com.mzpai.util.StringUtils;

@Service
public class OperateFlowLogServiceImpl extends BaseServiceImpl<OperateFlowLog> implements OperateFlowLogService {
	@Autowired
	private OperateFlowLogMapper  operateFlowLogMapper;
	@Autowired
	private OperateLogService  operateLogService;
	
	protected Logger logger = LoggerFactory.getLogger(OperateFlowLogServiceImpl.class);

	@Override
	public void saveFlowInfo(String nowTime) {
		Date endDate = DateUtil.getFormatHHNow();
		if(StringUtils.isNoneBlank(nowTime)) {
			endDate = DateUtil.formatDateTime(nowTime, DateUtil.DATE_HH_MM_SS);
		}
		logger.info("msg1=保存流量参数时间={}", DateUtil.formatDate(endDate, DateUtil.DATE_HH_MM_SS));
		/**初始化流量表；如果当天没有记录， insert； */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createDate", DateUtil.formatDate(endDate));
		List<OperateLog> list = operateLogService.getListByDate(map);
		
		/**初始化记录*/
		saveFlowLogInit(list, map);
		/**保存uv*/
		Map<String, OperateLog> totalMap = addTotalMap(list);
		saveDayInfo(totalMap, endDate);
			
	}
	private Map<String, OperateLog> addTotalMap(List<OperateLog> list) {
		Map<String, OperateLog> totalMap = new HashMap<String,OperateLog>();
		for(OperateLog o : list) {
			totalMap.put(DateUtil.formatDate(o.getCreateDate()) + ";" + o.getKindCode() + ";" + o.getStoreId(), o);
		}
		return totalMap;
	}
	/**
	 * 初始化； 没有数据 insert
	 * @param list
	 */
	private void saveFlowLogInit(List<OperateLog> list, Map<String, Object> map) {
		if(!list.isEmpty()) {
			List<OperateFlowLog> smptList = null;
			for(OperateLog o : list) {
				map.put("kindCode", o.getKindCode());
				map.put("storeId", o.getStoreId());
				map.put("createTime", map.get("createDate"));
				smptList = operateFlowLogMapper.getList(map);
				if(smptList.isEmpty() || smptList.size() == 0) {
					insertFlowLog(o);
				}
			}
		}
	}

	/*private void saveFlowLogInit(Date date) {
		String toDate = DateUtil.formatDate(date);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createTime", toDate);
		List<OperateLog> list = operateLogService.getListByDate(map);
		if(!list.isEmpty()) {
			List<OperateFlowLog> smptList = null;
			for(OperateLog o : list) {
				map.put("kindCode", o.getKindCode());
				smptList = mapper.getList(map);
				if(smptList.isEmpty() || smptList.size() == 0) {
					insertFlowLog(o);
				}
			}
		}
	}*/

	private void insertFlowLog(OperateLog o) {
		OperateFlowLog log = new OperateFlowLog();
		log.setKindCode(o.getKindCode());
		log.setCreateTime(o.getCreateDate());
		log.setStoreId(o.getStoreId());
		log.setStoreName(o.getStoreName());
		operateFlowLogMapper.insert(log);
		
	}

	/**
	 * 保存
	 */
	private void saveDayInfo(Map<String, OperateLog> totalMap, Date date) {
		Date beginDate = DateUtil.beforeMin(date, 60);
		String toDate = DateUtil.formatDate(date);
		logger.info("每小时统计整点时间流量， 参数, beginDate={}, endDate ={}", DateUtil.formatDate(beginDate, DateUtil.DATE_HH_MM_SS), DateUtil.formatDate(date, DateUtil.DATE_HH_MM_SS));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createDate", DateUtil.formatDate(beginDate, DateUtil.DATE));
		List<OperateLog> list = operateLogService.getListByHour(map);
		if(!list.isEmpty()) {
			Map<String, OperateFlowLog> mapV = getOperateLogGroup(list, toDate, totalMap);
			for(String key : mapV.keySet()){
				/**添加流量数据**/
				operateFlowLogMapper.updateByFlowLog(mapV.get(key));
            }
		}
	}

	/**
	 * 获取类型分组
	 * @param list
	 * @return
	 */
	private Map<String, OperateFlowLog> getOperateLogGroup(List<OperateLog> list, String toDate, Map<String, OperateLog> totalMap) {
		Map<String, OperateFlowLog> map = new HashMap<String, OperateFlowLog>();
		for(OperateLog s : list) {
			if(map.containsKey(toDate +";" + s.getKindCode() + ";" + s.getStoreId())) {
				OperateFlowLog vo = map.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId());
				addFlowLogHour(vo, s, false);
				map.put(toDate +";" + s.getKindCode() + ";" + s.getStoreId(), vo);
			} else {
				OperateFlowLog vo = new OperateFlowLog();
				addFlowLogHour(vo, s, true);
				logger.info("msg1=totalMap信息如下：" + totalMap);
				vo.setTotal(totalMap.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId()) == null ? 0 : totalMap.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId()).getTotal());
				vo.setLastWeekTotal(totalMap.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId()) == null ? 0 : totalMap.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId()).getLastWeekTotal());
				vo.setLastDayTotal(totalMap.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId()) == null ? 0 : totalMap.get(toDate +";" + s.getKindCode() + ";" + s.getStoreId()).getLastDayTotal());
				map.put(toDate +";" + s.getKindCode() + ";" + s.getStoreId(), vo);

			}
		}
		return map;
	}

	/***
	 * 根据 kindcode, stroreid, createtime 
	 * @param vo
	 * @param s
	 */
	private void addFlowLogHour(OperateFlowLog vo, OperateLog s, boolean b) {
		
		Date createTime=s.getCreateTime();
		String createTimeStr=DateUtil.parseDate(createTime);
		
		if(b) {
			vo.setKindCode(s.getKindCode());
			Date careateDate=DateUtil.formatDate(createTime.getTime(), DateUtil.DATE);
			vo.setCreateTime(careateDate);
			vo.setStoreId(s.getStoreId());
		}
		
		String sTime = createTimeStr.substring(11,13);
		switch (sTime) {
			case "00":
				vo.setH1(s.getTotal());
				break;
			case "01":
				vo.setH2(s.getTotal());
				break;
			case "02":
				vo.setH3(s.getTotal());
				break;
			case "03":
				vo.setH4(s.getTotal());
				break;
			case "04":
				vo.setH5(s.getTotal());
				break;
			case "05":
				vo.setH6(s.getTotal());
				break;
			case "06":
				vo.setH7(s.getTotal());
				break;
			case "07":
				vo.setH8(s.getTotal());
				break;
			case "08":
				vo.setH9(s.getTotal());
				break;
			case "09":
				vo.setH10(s.getTotal());
				break;
			case "10":
				vo.setH11(s.getTotal());
				break;
			case "11":
				vo.setH12(s.getTotal());
				break;
			case "12":
				vo.setH13(s.getTotal());
				break;
			case "13":
				vo.setH14(s.getTotal());
				break;
			case "14":
				vo.setH15(s.getTotal());
				break;
			case "15":
				vo.setH16(s.getTotal());
				break;
			case "16":
				vo.setH17(s.getTotal());
				break;
			case "17":
				vo.setH18(s.getTotal());
				break;
			case "18":
				vo.setH19(s.getTotal());
				break;
			case "19":
				vo.setH20(s.getTotal());
				break;
			case "20":
				vo.setH21(s.getTotal());
				break;
			case "21":
				vo.setH22(s.getTotal());
				break;
			case "22":
				vo.setH23(s.getTotal());
				break;
			case "23":
				vo.setH24(s.getTotal());
				break;
			default:
				break;
			}
	}
	
	@Override
	public List<OperateFlowLog> getFlowLogListGroupByKindCodeCreateTime(OperateFlowLogReqVo operateFlowReqVo) {
		String startDateStr=operateFlowReqVo.getStartDate();
		String endDateStr=operateFlowReqVo.getEndDate();
		String storeId=operateFlowReqVo.getStoreId();
		String kindCode=operateFlowReqVo.getKindCode();
		//结束时间为空 则默认和开始时间一样只查当天
        if(StringUtils.isNullOrBlank(endDateStr)){
        	endDateStr=startDateStr;
		}
		String startStr=DateUtil.formatDateStr(startDateStr,DateUtil.DATE); 
        String endStr=DateUtil.formatDateStr(endDateStr,DateUtil.DATE); 
        
		Map<String,Object> operateFlowLogMap=new HashMap<String,Object>();
		operateFlowLogMap.put("startDate", startStr);
		operateFlowLogMap.put("endDate", endStr);
		operateFlowLogMap.put("kindCode", kindCode);
		operateFlowLogMap.put("storeId", storeId);
		List<OperateFlowLog> listOperateFlowLog=operateFlowLogMapper.getFlowLogListGroupByKindCodeCreateTime(operateFlowLogMap);
		return listOperateFlowLog;
	}
	@Override
	public List<OperateFlowLog> getFlowLogListGroupByStoreIdKindCodeCreateTime(OperateFlowLogReqVo operateFlowReqVo) {
		String startDateStr=operateFlowReqVo.getStartDate();
		String endDateStr=operateFlowReqVo.getEndDate();
		String storeId=operateFlowReqVo.getStoreId();
		String kindCode=operateFlowReqVo.getKindCode();
		//结束时间为空 则默认和开始时间一样只查当天
        if(StringUtils.isNullOrBlank(endDateStr)){
        	endDateStr=startDateStr;
		}
		String startStr=DateUtil.formatDateStr(startDateStr,DateUtil.DATE); 
        String endStr=DateUtil.formatDateStr(endDateStr,DateUtil.DATE); 
        
		Map<String,Object> operateFlowLogMap=new HashMap<String,Object>();
		operateFlowLogMap.put("startDate", startStr);
		operateFlowLogMap.put("endDate", endStr);
		operateFlowLogMap.put("kindCode", kindCode);
		operateFlowLogMap.put("storeId", storeId);
		List<OperateFlowLog> listOperateFlowLog=operateFlowLogMapper.getFlowLogListGroupByStoreIdKindCodeCreateTime(operateFlowLogMap);
		return listOperateFlowLog;
	}
	
}
